import { urls } from '@/router/url'
import cache from '@/utils/cache'
import type { MenuTree, MenuItem } from '@/api/back-management/menu/interface'

const PERMISSIONS = {
  // 顶部菜单
  首页: 'digtalboard',
  体检调查信息管理: 'map',
  指标数据管理: 'index',
  成果展示: 'result',
  后台管理: 'backstage',
  更新项目管理: 'projectUpdate',
  大屏管理: 'screenManagement',

  // 指标数据管理
  基础指标: 'index::index::base',
  特色指标: 'index::index::special',
  任务管理: 'index::publish',
  数据填报: 'index::fillIn',
  报告生成: 'index::release',
  报告上传: 'index::report',


  // 大屏管理
  指标管理: 'screenManagement::indicatorManagement',
  大屏配置: 'screenManagement::screenConfig',
  指标配置: 'screenManagement::indicatorConfig',
  汇报平台: 'screenManagement::reportPlatform',
  项目管理: 'screenManagement::reportPlatform::projectManagement',
  模板管理: 'screenManagement::reportPlatform::templateManagement',

  // 后台管理
  用户管理: 'backstage::user::user',
  会话管理: 'backstage::user::conversation',
  黑名单管理: 'backstage::user::blacklist',
  部门管理: 'backstage::dept',
  角色管理: 'backstage::role',
  菜单管理: 'backstage::menu',
  登录日志管理: 'backstage::log::login',
  操作日志记录: 'backstage::log::operate',
  底图资源管理: 'backstage::resource::basemap',
  数据目录管理: 'backstage::resource::catalog',

  // 部门管理按钮
  部门管理新增: 'backstage::dept::add',
  部门管理编辑: 'backstage::dept::edit',
  部门管理删除: 'backstage::dept::delete',
  部门管理导出: 'backstage::dept::export',

  // 角色管理按钮
  角色管理新增: 'backstage::role::add',
  角色管理编辑: 'backstage::role::edit',
  角色管理删除: 'backstage::role::delete',

  // 菜单管理按钮
  菜单管理新增: 'backstage::menu::add',
  菜单管理编辑: 'backstage::menu::edit',
  菜单管理删除: 'backstage::menu::delete',

  // 底图资源管理按钮
  底图资源管理新增: 'backstage::resource::basemap::add',
  底图资源管理编辑: 'backstage::resource::basemap::edit',
  底图资源管理删除: 'backstage::resource::basemap::delete',

  // 基础指标管理按钮
  基础指标新增体系: 'index::index::base::add',
  基础指标编辑: 'index::index::base::edit',
  基础指标删除: 'index::index::base::delete',
  基础指标详情: 'index::index::base::detail',
  基础指标保存: 'index::index::base::save',
  基础指标发布: 'index::index::base::publish',

  // 特色指标管理按钮
  特色指标新增体系: 'index::index::special::add',
  特色指标编辑: 'index::index::special::edit',
  特色指标删除: 'index::index::special::delete',
  特色指标详情: 'index::index::special::detail',
  特色指标取消发布: 'index::index::special::cancel',
  特色指标保存: 'index::index::special::save',
  特色指标发布: 'index::index::special::publish',

  // 任务管理按钮
  任务管理新增: 'index::publish::add',
  任务管理编辑: 'index::publish::edit',
  任务管理删除: 'index::publish::delete',
  任务管理详情: 'index::publish::detail',
  任务管理取消发布: 'index::publish::cancel',
  任务管理保存: 'index::publish::save',
  任务管理发布: 'index::publish::publish',

  // 数据填报按钮
  数据填报详情: 'index::fillIn::detail',
  数据填报退回: 'index::fillIn::back',
  数据填报填报: 'index::fillIn::fill',
  数据填报下载: 'index::fillIn::download',
  数据填报保存: 'index::fillIn::save',
  数据填报提交: 'index::fillIn::submit',
  数据填报通过: 'index::fillIn::pass',

  // 报告生成按钮
  报告生成导出数据: 'index::release::export',
  报告生成详情: 'index::release::detail',
  报告生成填报: 'index::release::fill',
  报告生成下载: 'index::release::download',
  报告生成预览: 'index::release::preview',
  报告生成保存: 'index::release::save',

  // 报告上传按钮
  报告上传上传: 'index::report::upload',

  // 一张图按钮
  一张图下载: 'map::download',
  一张图表格下载: 'map::table::download',
  一张图批量退回: 'map::batch::back',
  一张图专项文件上传: 'map::special::upload',
  一张图审核: 'map::audit',
  一张图楼宇编辑: 'map::building::edit',
  一张图隐患楼宇统计: 'map::building::danger',

  // 更新项目管理
  入库审核: 'projectUpdate::inventoryReview',
  片区入库: 'projectUpdate::inventoryReview::area',
  项目入库: 'projectUpdate::inventoryReview::project',
  子项目入库: 'projectUpdate::inventoryReview::subProject',
  项目库管理: 'projectUpdate::libraryManagement',
  年度计划: 'projectUpdate::annualPlan',
  项目月报: 'projectUpdate::monthReport',
  项目台账: 'projectUpdate::projectLedger',
  总台账: 'projectUpdate::projectLedger::general',
  入统台账: 'projectUpdate::projectLedger::unified',
  金融台账: 'projectUpdate::projectLedger::financial',

  //更新项目管理按钮
  片区入库新增: 'projectUpdate::inventoryReview::area::add',
  片区入库编辑: 'projectUpdate::inventoryReview::area::edit',
  片区入库删除: 'projectUpdate::inventoryReview::area::delete',
  片区入库详情: 'projectUpdate::inventoryReview::area::detail',
  片区入库通过: 'projectUpdate::inventoryReview::area::pass',
  片区入库退回: 'projectUpdate::inventoryReview::area::back',
  片区入库变更: 'projectUpdate::inventoryReview::area::change',
  片区入库审核: 'projectUpdate::inventoryReview::area::audit',
  项目入库新增: 'projectUpdate::inventoryReview::project::add',
  项目入库编辑: 'projectUpdate::inventoryReview::project::edit',
  项目入库删除: 'projectUpdate::inventoryReview::project::delete',
  项目入库详情: 'projectUpdate::inventoryReview::project::detail',
  项目入库通过: 'projectUpdate::inventoryReview::project::pass',
  项目入库退回: 'projectUpdate::inventoryReview::project::back',
  项目入库变更: 'projectUpdate::inventoryReview::project::change',
  项目入库审核: 'projectUpdate::inventoryReview::project::audit',
  子项目入库新增: 'projectUpdate::inventoryReview::subProject::add',
  子项目入库编辑: 'projectUpdate::inventoryReview::subProject::edit',
  子项目入库删除: 'projectUpdate::inventoryReview::subProject::delete',
  子项目入库详情: 'projectUpdate::inventoryReview::subProject::detail',
  项目库管理详情: 'projectUpdate::libraryManagement::detail',
  项目库管理编辑: 'projectUpdate::libraryManagement::edit',

  年度计划新增: 'projectUpdate::annualPlan::add',
  年度计划编辑: 'projectUpdate::annualPlan::edit',
  年度计划删除: 'projectUpdate::annualPlan::delete',
  年度计划详情: 'projectUpdate::annualPlan::detail',
  项目月报详情: 'projectUpdate::monthReport::detail',
  项目月报下载: 'projectUpdate::monthReport::download',
  项目月报编辑: 'projectUpdate::monthReport::edit',
  项目月报删除: 'projectUpdate::monthReport::delete',
  项目月报退回: 'projectUpdate::monthReport::back',
  年度计划下载: 'projectUpdate::annualPlan::download',
  总台账列表下载: 'projectUpdate::projectLedger::general::download',
  总台账详情下载: 'projectUpdate::projectLedger::general::detail::download',
  总台账详情: 'projectUpdate::projectLedger::general::detail',
  入统台账列表下载: 'projectUpdate::projectLedger::unified::download',
  入统台账详情下载: 'projectUpdate::projectLedger::unified::detail::download',
  入统台账详情: 'projectUpdate::projectLedger::unified::detail',
  金融台账列表下载: 'projectUpdate::projectLedger::financial::download',
  金融台账详情下载: 'projectUpdate::projectLedger::financial::detail::download',
  金融台账详情: 'projectUpdate::projectLedger::financial::detail',
}

const ALL_MENU_PATHS: { key: string; path: string | any }[] = [
  {
    key: PERMISSIONS.首页,
    path: urls.digtalboard,
  },
  {
    key: PERMISSIONS.体检调查信息管理,
    path: urls.map,
  },
  {
    key: PERMISSIONS.基础指标,
    path: urls.index.index.base,
  },
  {
    key: PERMISSIONS.特色指标,
    path: urls.index.index.special,
  },
  {
    key: PERMISSIONS.任务管理,
    path: urls.index.publish,
  },
  {
    key: PERMISSIONS.数据填报,
    path: urls.index.fillIn,
  },
  {
    key: PERMISSIONS.报告生成,
    path: urls.index.release,
  },
  {
    key: PERMISSIONS.报告上传,
    path: urls.index.report,
  },
  {
    key: PERMISSIONS.成果展示,
    path: urls.result,
  },
  {
    key: PERMISSIONS.用户管理,
    path: urls.backstage.user.user.list,
  },
  {
    key: PERMISSIONS.会话管理,
    path: urls.backstage.user.conversation.list,
  },
  {
    key: PERMISSIONS.黑名单管理,
    path: urls.backstage.user.blacklist.list,
  },
  {
    key: PERMISSIONS.部门管理,
    path: urls.backstage.dept.list,
  },
  {
    key: PERMISSIONS.角色管理,
    path: urls.backstage.role.list,
  },
  {
    key: PERMISSIONS.菜单管理,
    path: urls.backstage.menu.list,
  },
  {
    key: PERMISSIONS.底图资源管理,
    path: urls.backstage.data.basemap.list,
  },
  {
    key: PERMISSIONS.数据目录管理,
    path: urls.backstage.data.catalog.list,
  },
  {
    key: PERMISSIONS.片区入库,
    path: urls.projectUpdate.inventoryReview.area,
  },
  {
    key: PERMISSIONS.项目入库,
    path: urls.projectUpdate.inventoryReview.project,
  },
  {
    key: PERMISSIONS.子项目入库,
    path: urls.projectUpdate.inventoryReview.subProject,
  },
  {
    key: PERMISSIONS.项目库管理,
    path: urls.projectUpdate.libraryManagement,
  },
  {
    key: PERMISSIONS.年度计划,
    path: urls.projectUpdate.annualPlan,
  },
  {
    key: PERMISSIONS.项目月报,
    path: urls.projectUpdate.monthReport,
  },
  {
    key: PERMISSIONS.总台账,
    path: urls.projectUpdate.projectLedger.general,
  },
  {
    key: PERMISSIONS.入统台账,
    path: urls.projectUpdate.projectLedger.unified,
  },
  {
    key: PERMISSIONS.金融台账,
    path: urls.projectUpdate.projectLedger.financial,
  },
  {
    key: PERMISSIONS.指标管理,
    path: urls.screenManagement.indicatorManagement,
  },
  {
    key: PERMISSIONS.大屏配置,
    path: urls.screenManagement.screenConfig,
  },
  {
    key: PERMISSIONS.指标配置,
    path: urls.screenManagement.indicatorConfig,
  },
  {
    key: PERMISSIONS.汇报平台,
    path: urls.screenManagement.reportPlatform,
  },
  {
    key: PERMISSIONS.项目管理,
    path: urls.screenManagement.reportPlatform.projectManagement,
  },
  {
    key: PERMISSIONS.模板管理,
    path: urls.screenManagement.reportPlatform.templateManagement,
  }
]

class CurrentUser {
  moduleTree: MenuTree[] = []
  permissionTree: MenuTree[] = cache.getItem('city-permissionButtonList')
    ? JSON.parse(
        cache.getItem('city-permissionButtonList')?.toString() as string
      )
    : []

  hasPermission(permission: string): boolean {
    const obj = this.permissionTree.find((i: MenuTree) => i.code === permission)
    return (obj?.hasPri && obj?.status === 1) as boolean
  }

  getButtonName(permission: string): string {
    return (
      this.permissionTree.filter(
        (i: MenuTree) => i.code === permission && i.hasPri && i.status === 1
      ).length
        ? this.permissionTree.filter((i: MenuTree) => i.code === permission)[0]
            .catalogName
        : ''
    ) as string
  }

  getMenuPath(): string {
    this.moduleTree = JSON.parse(
      cache.getItem('city-permission')?.toString() as string
    )
    this.permissionTree = JSON.parse(
      cache.getItem('city-permissionButtonList')?.toString() as string
    )
    // if (this.moduleTree && this.moduleTree.length && menuCode) {
    //   const child = this.moduleTree[0].children.find(
    //     (i: MenuTree) => i.address === menuCode && i.hasPri && i.status === '1'
    //   ) as MenuTree
    //   if (child.children && child.children.length) {
    //     let address = ''
    //     const select = child.children.find(
    //       (i: MenuTree) => i.hasPri && i.status === '1'
    //     )

    //     if (
    //       select?.children.length &&
    //       select?.children[0].catalogType === '1' &&
    //       select?.children.find((i: MenuTree) => i.hasPri && i.status === '1')
    //     ) {
    //       // 左侧目录两层
    //       address = select?.children.find(
    //         (i: MenuTree) => i.hasPri && i.status === '1'
    //       )?.address as string
    //     } else {
    //       // 左侧目录一层
    //       address = select?.address as string
    //     }
    //     let find = ALL_MENU_PATHS.find((x) => x.key == address)?.path || ''
    //     if (!find) {
    //       find = ALL_MENU_PATHS.find((x) => x.key == menuCode)?.path || ''
    //     }
    //     return find
    //   } else {
    //     return ALL_MENU_PATHS.find((x) => x.key == menuCode)?.path || ''
    //   }
    // }
    return ''
  }

  getSidePath(menuCode: string): string {
    if (menuCode) {
      return ALL_MENU_PATHS.find((x) => x.key == menuCode)?.path || ''
    }
    return ''
  }

  getSideMenuList(tree: MenuTree[]) {
    const arr = [] as MenuItem[]
    tree.forEach((item) => {
      if (
        item.children.length &&
        item.children[0].catalogType === '1' &&
        item.hasPri &&
        item.status === 1
      ) {
        item.children = this.getSideMenuList(item.children) as any
      }

      if (item.hasPri && item.status === 1) {
        arr.push({
          key: item.address || '',
          name: item.catalogName || '',
          children:
            item.children.length && item.children[0].catalogType !== '0'
              ? (item.children as MenuItem[])
              : [],
          hasChildren:
            item.children.length && item.children[0].catalogType !== '0'
              ? true
              : false,
          path: this.getSidePath(item.address as string),
          icon: item.icon,
          choosePicture: item.choosePicture,
          unChoosePicture: item.unChoosePicture,
        })
      }
    })
    return arr
  }

  getMenuKey(path: string): string {
    if (path) {
      return ALL_MENU_PATHS.find((x) => x.path == path)?.key || ''
    }
    return ''
  }
}

export { PERMISSIONS, CurrentUser }
